package org.hgvc.webapp.ets.jsf.beans.mastersetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.Status;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class StatusBean extends AbstractBean {
	public static final String BEAN_NAME="statusBean";
	private MasterSetupService service;
	private List<Status> statusList;
	private Status status;
	public static final String NAV_LIST_STATUS="listStatus";
	public static final String NAV_ADD_STATUS="addStatus";
	public static final String NAV_EDIT_STATUS="editStatus";
	 
	public MasterSetupService getService() {
		return service;
	}
	public void setService(MasterSetupService service) {
		this.service = service;
	}
	public Status getStatus() {
		return status;
	}
	public void setStatus(Status status) {
		this.status = status;
	}
	public List<Status> getStatusList() {
		try{
			statusList = service.getStatusList();
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return statusList;
	}
	public void setStatusList(List<Status> statusList) {
		this.statusList = statusList;
	}
	
	public String navCreationAction(){
		status = new Status();
		return NAV_ADD_STATUS;
	}
	
	public String createAction(){
		try{
			status.setCreatedBy(getThisUser());
			status.setCreatedDate(new Date());
			status.setIsDeleted(CommonConstant.NOT_DELETED);
			service.saveStatus(status);
			return NAV_LIST_STATUS;
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return NAV_ADD_STATUS;
	}
	
	public void editListener(ActionEvent actionEvent){
		status = null;
		UIParameter parameter = (UIParameter)actionEvent.getComponent().findComponent("statusId");
		String statusId = (String)parameter.getValue();
		for(Status a:statusList){
			if(a.getStatusId().equalsIgnoreCase(statusId)){
				status = a;
				break;
			}
		}
	}
	
	public String navEditAction(){
		return NAV_EDIT_STATUS;
	}
	
	public String editAction(){
		try{
			status.setModifiedBy(getThisUser());
			status.setModifiedDate(new Date());
			service.updateStatus(status);
			return NAV_LIST_STATUS;
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		
		return NAV_EDIT_STATUS;
	}
	
	public String deleteAction(){
		try{
			
			List<Status> deleteList = new ArrayList<Status>();
			for(Status a:statusList){
				if(a.isMarkAsAction()){
					deleteList.add(a);
				}
			}
			service.deleteStatuses(deleteList);
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return NAV_LIST_STATUS;
	}
}
